package de.pernpas.calendar.common;

import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.logging.Logger;

public class CalUtils {

	private final Logger log = Logger.getLogger(CalUtils.class.getName());
	
	public CalUtils(){}
	
	public Date getEndDay(Date date) {
		log.fine("Hole Enddatum zu " + date);
		GregorianCalendar cal = new GregorianCalendar(Locale.GERMANY);
		cal.setTime(date);
		int actMonth = cal.get(Calendar.MONTH);

		while(cal.get(Calendar.MONTH) == actMonth ||
				cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY){
			Date myDate = addHours(cal.getTime(), 24);
			cal.setTime(myDate);
		}
		
		log.fine("Enddatum: " + cal.getTime());
		return cal.getTime();
	}

	public Date getStartDay(Date date) {
		log.fine("Hole Startdatum zu " + date);
		GregorianCalendar cal = new GregorianCalendar(Locale.GERMANY);
		cal.setTime(date);
		int actMonth = cal.get(Calendar.MONTH);

		while(cal.get(Calendar.MONTH) == actMonth ||
				cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY){
			Date myDate = addHours(cal.getTime(), -24);
			cal.setTime(myDate);
		}
		
		log.fine("Startdatum: " + cal.getTime());
		return cal.getTime();
	}
	
	public Date addHours(Date date, int hours){
		GregorianCalendar cal = new GregorianCalendar(Locale.GERMANY);
		cal.setTime(date);
		cal.add(Calendar.HOUR, hours);
		return cal.getTime();
	}
	
	public Date addMinutes(Date date, int minutes){
		GregorianCalendar cal = new GregorianCalendar(Locale.GERMANY);
		cal.setTime(date);
		cal.add(Calendar.MINUTE, minutes);
		return cal.getTime();
	}

	public Date getFirstMinuteInMonth(int month, int year) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, cal.getMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}

	public Date getLastMinuteInMonth(int month, int year) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	public Date getFirstMinuteOfDay(int day, int month, int year) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	public Date getLastMinuteOfDay(int day, int month, int year) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}

	/**
	 * Formatiert String in Date
	 * @param dateString
	 * @return
	 */
	public Date getDate(String dateString) {
		Date  date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy");
		try {
			date = sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * Formatiert String in Date
	 * @param dateString im Format "dd.MM.yy HH:mm"
	 * @return
	 */
	public Date getDateWithTime(String dateString) {
		Date  date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy HH:mm");
		try {
			date = sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * Liefert einen String der Form dd.MM.yyyy
	 * @param date
	 * @return
	 */
	public String getDateString(Date date){
		SimpleDateFormat sdf= new SimpleDateFormat("dd.MM.yyyy");
		return sdf.format(date);
	}
	
	public String getMonthForInt(int m) {
		if(m < 0 || m > 11){
			throw new IllegalArgumentException("Value must be between 0 and 11, but was " + m);
		}
		String month = "";
	    DateFormatSymbols dfs = new DateFormatSymbols();
	    String[] months = dfs.getMonths();
	    if (m >= 0 && m <= 11 ) {
	        month = months[m];
	    }
	    return month;
	}
	
}
